package controllers

import (
	"beegodemo03/models"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type BoysController struct {
	beego.Controller
}

func (ctl *BoysController) List() {
	query := orm.NewOrm().QueryTable(new(models.Boys))
	boyList := make([]models.Boys, 0)
	_, _ = query.All(&boyList)
	fmt.Println(boyList)

	resp := models.RespObj{
		Code: 200,
		Msg:  "获取boys列表成功",
		Data: boyList,
	}
	ctl.Data["json"] = resp
	ctl.ServeJSON()
}

// 根据id查询
func (ctl *BoysController) GetById() {
	id, _ := ctl.GetInt(":id")
	fmt.Println("请求入参: ", id)
	query := orm.NewOrm().QueryTable(new(models.Boys)).Filter("id", id)
	m := new(models.Boys)
	err := query.One(m)
	var resp models.RespObj
	if err != nil {
		fmt.Println("查询失败!")
		resp = models.Fail(500, "boy不存在")
	} else {
		resp = models.Success(200, m)
	}

	ctl.Data["json"] = resp
	ctl.ServeJSON()
}
